package com.xm.servlet.reception_desk.order_settlement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 计算折扣
 * @author 康文程
 * @date 2024-1-8 上午 10:58
 */
@WebServlet("/order/discount")
public class OrderDiscount extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BigDecimal discount = new BigDecimal(req.getParameter("discount"));
        BigDecimal total = new BigDecimal(req.getParameter("total"));

        BigDecimal discountedPrice = total.multiply(discount);
        BigDecimal discountAmount = total.subtract(discountedPrice);

        // 使用 DecimalFormat 将结果格式化为保留两位小数的字符串
        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
        String formattedDiscountedPrice = decimalFormat.format(discountedPrice);
        String formattedDiscountAmount = decimalFormat.format(discountAmount);
        String add = formattedDiscountedPrice+","+formattedDiscountAmount;
        resp.getWriter().println(add);
    }
}
